﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using GuestList.Commands;
using System.Windows.Controls;
using GuestList.Models;
using System.Windows.Media;

namespace GuestList.ViewModels
{
    public class PrintPreviewVM : WindowVM
    {
        private Invitation _model;
        private string _addressLines = string.Empty;
        private bool _hideLetter = false;

        public new PrintPreviewWindow Window
        {
            get { return base.Window as PrintPreviewWindow; }
            set { base.Window = value; }
        }

        protected override Window GetNewWindowInstance()
        {
            return new PrintPreviewWindow(); ;
        }

        public Invitation Model
        {
            get { return _model; }
            set
            {
                _model = value;
                PopulateDefaultAddressLines();
                Notify("Model");
            }
        }

        public FontFamily FontFamily
        {
            get { return UserSettings.Default.PrintFontFamily; }
            set
            {
                UserSettings.Default.PrintFontFamily = value;
                Notify("FontFamily");
            }
        }

        public double FontSize
        {
            get { return UserSettings.Default.PrintFontSize; }
            set
            {
                UserSettings.Default.PrintFontSize = value;
                Notify("FontSize");
            }
        }

        public FontStyle FontStyle
        {
            get { return UserSettings.Default.PrintFontStyle; }
            set
            {
                UserSettings.Default.PrintFontStyle = value;
                Notify("FontStyle");
            }
        }

        public FontCapitals FontCapitals
        {
            get { return UserSettings.Default.PrintFontCapitals; }
            set
            {
                UserSettings.Default.PrintFontCapitals = value;
                Notify("FontCapitals");
            }
        }

        public string AddressLines
        {
            get { return _addressLines; }
            set
            {
                _addressLines = value;
                Notify("AddressLines");
            }
        }

        public bool HideLetter
        {
            get { return _hideLetter; }
            set
            {
                _hideLetter = value;
                Notify("HideLetter");
                Notify("Ceremony");
            }
        }

        public string Ceremony
        {
            get
            {
                if (HideLetter)
                {
                    return string.Empty;
                }
                if (Model.InvitationType == InvitationType.Ceremony)
                {
                    return "C";
                }
                else
                {
                    return "R";
                }
            }
        }

        public PrintPreviewVM(PrintPreviewWindow window) : base(window)
        {

        }

        public RelayCmd PrintCmd { get { return new RelayCmd(PrintInvitation, () => true); } }

        private void PrintInvitation()
        {
            PrintDialog printDialog = new PrintDialog();
            if (printDialog.ShowDialog() == true)
            {
                printDialog.PrintVisual(Window.Grid, Model.PrimaryGuest.ToString());
            }
        }

        public RelayCmd SuccessCmd
        {
            get
            {
                return new RelayCmd(() =>
                {
                    Model.HavePrintedInvitation = true;
                    Window.Close();
                }, 
                () => true);
            }
        }

        private void PopulateDefaultAddressLines()
        {
            Queue<Person> people = new Queue<Person>();
            people.Enqueue(Model.PrimaryGuest);
            foreach (var person in Model.OtherGuests)
            {
                people.Enqueue(person);
            }
            StringBuilder sb = new StringBuilder();
            sb.Append(people.Dequeue().ToString());
            while (people.Count > 1)
            {
                sb.AppendFormat(", {0}", people.Dequeue());
            }
            if (people.Count == 1)
            {
                sb.AppendFormat(" and {0}", people.Dequeue());
            }
            sb.AppendLine();
            if (!string.IsNullOrWhiteSpace(Model.Address1))
            {
                sb.AppendLine(Model.Address1);
            }
            if (!string.IsNullOrWhiteSpace(Model.Address2))
            {
                sb.AppendLine(Model.Address2);
            }
            sb.AppendFormat("{0}, {1}", Model.City, Model.State);
            sb.AppendLine();
            sb.Append(Model.ZipCode);
            AddressLines = sb.ToString();
        }
    }
}
